// components/view-item/view-item.js
import IndexModel from "../../models/IndexModel";
const indexModel = new IndexModel();
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    type: {
      type: String,
      value: 'info', // info, package, (primary)
    },
    gallery: {
      type: Array,
      value: [],
    },
    infoData: {
      type: Object,
      value: {},
    },
    sel: {
      type: Boolean,
      value: false,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    showDesc: false,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    clickShowDesc() {
      let {
        id,
        type
      } = this.handleType();
      indexModel.judgeFavor(id, type, app.globalData.userId).then(res => {
        // console.log('favor1111 ->', res);
        let sel = res && res.IsMark === '1';
        this.setData({
          showDesc: !this.data.showDesc,
          sel,
        })
      })
    },
    skipDetail() {
      if (this.data.type === 'package') {
        wx.navigateTo({
          url: `../detail/detail?id=${this.data.infoData.Jinnang_Id}&tableName=jyJinnang`,
        })
      } else {
        wx.navigateTo({
          url: `../detail/detail?id=${this.data.infoData.Zixun_Id}&tableName=jyZixun`,
        })
      }
    },
    clickFavor(e) {
      let sel = e.detail.sel;
      let {
        id,
        type
      } = this.handleType();
      indexModel.favor('1', type, id, sel, app.globalData.userId).then(res => {
        // console.log('favor22222->', res);
        wx.showToast({
          title: '成功',
          icon: 'success',
          duration: 2000,
        })
      })
    },
    handleType() {
      let data = this.data.infoData;
      let id, type;
      if (this.data.type === 'package') {
        id = data.Jinnang_Id;
        type = '3';
      } else {
        id = data.Zixun_Id;
        type = '2';
      }
      return {id, type}
    }
  }
})